package com.wanxi.servlet.device;

import com.alibaba.fastjson.JSONObject;
import com.wanxi.VO.DeviceVO;
import com.wanxi.VO.UserVO;
import com.wanxi.dto.ResultDTO;
import com.wanxi.service.DeviceService;
import com.wanxi.service.impl.DeviceServiceImpl;
import com.wanxi.service.impl.UserServiceImpl;
import com.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : 林子淳
 * @create 2023/2/28 8:50
 */
//@WebServlet("/back/device/add")
public class DeviceAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1、乱码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //2、获取前端传过来的值
        String device_class_id = req.getParameter("device_class_id");
        String name = req.getParameter("name");
        String normalPrice = req.getParameter("normalPrice");
        String imgHref = req.getParameter("imgHref");
        String remark = req.getParameter("content");
        String enable = req.getParameter("enable");
        //3.封装VO数据
        DeviceVO deviceVO = new DeviceVO();
        deviceVO.setDevice_class_id(Tool.strToInt(device_class_id));
        deviceVO.setName(Tool.nullToStr(name));
        deviceVO.setNormalPrice(Tool.strToBigDecimal(normalPrice));
        deviceVO.setImgHref(Tool.nullToStr(imgHref));
        deviceVO.setContent(Tool.nullToStr(remark));
        deviceVO.setEnable(Tool.strToInt(enable));

        // 4.调用服务逻辑层
        DeviceService productService = new DeviceServiceImpl();
        //        5.得到返回的结果
        ResultDTO resultDTO = productService.add(deviceVO);
        //        6.返回给前端
        resp.getWriter().println(JSONObject.toJSONString(resultDTO));
    }


}
